#include "dia_addfood.h"
#include "ui_dia_addfood.h"
#include "foodsql.h"
#include <QMessageBox>
Dia_AddFood::Dia_AddFood(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dia_AddFood)
{
    ui->setupUi(this);
}

Dia_AddFood::~Dia_AddFood()
{
    delete ui;
}

void Dia_AddFood::setType(bool isAdd, FoodInfo info)
{
    m_isAdd = isAdd;
    m_info=info;
    ui->le_name->setText(info.name);
    ui->sb_number->setValue(info.num);
    ui->le_unit->setText(info.foodunit);
    ui->de_date->setDate(QDate::fromString(info.date));
}

void Dia_AddFood::on_btn_save_clicked()
{
    FoodInfo info;
    auto ptr = foodSql::getinstance();
    info.id=m_info.id;
    info.name=ui->le_name->text();
    info.num=ui->sb_number->text().toUInt();
    info.foodunit=ui->le_unit->text();
    info.date=ui->de_date->text();

    if(m_isAdd)
    {
        ptr->addFood(info);
    }
    else
    {
        ptr->updateFoodInfo(info);
    }

    QMessageBox::information(nullptr,"信息","存储成功");
    this->hide();
}


void Dia_AddFood::on_btn_cancel_clicked()
{
    this->hide();
}

